package com.charonmc.utils.mysql;

import java.sql.*;


/**
 * SQLManager是MySql操作核心，用于封装具体方法，管理数据连接
 * @author ChuYang
 */

public class SqlManager {
    PreparedStatement ps;
    public Connection connection;
    public ResultSet set;
    public StringBuilder sql = new StringBuilder();

    /***
     *
     * @param database 所需要操作的数据库
     * @throws SQLException 数据路异常
     */
    public SqlManager(String database) throws SQLException {
        String url = "jdbc:mysql://localhost:3306/" + database + "?&useUnicode=true&characterEncoding=utf8&useSSL=false";
        String user = "root";
        String password = "1P|6V@Alsjg=Rb4X";
        this.connection = DriverManager.getConnection(url, user, password);
    }

    public SqlManager(){}

    /***
     * 创建查询对象
     * @return {@link QueryUtils}
     */
    public QueryUtils createQuery() {
        QueryUtils qu = new QueryUtils(this.connection);
        qu.sql.append("SELECT ");
        return qu;
    }

    /***
     * 创建插入对象
     * @return {@link InsertUtils}
     */
    public InsertUtils createInsert() {
        InsertUtils iu = new InsertUtils(this.connection);
        iu.sql.append("INSERT INTO ");
        return iu;
    }

    /**
     * 创建更改对象
     * @return {@link UpdateUtils}
     */
    public UpdateUtils createUpdate() {
        UpdateUtils uu = new UpdateUtils(this.connection);
        uu.sql.append("UPDATE ");
        return uu;
    }

    /**
     * 创建删除对象
     * @return {@link DeleteUtils}
     */
    public DeleteUtils createDelete() {
        DeleteUtils du = new DeleteUtils(this.connection);
        du.sql.append("DELETE FROM ");
        return du;
    }

    /**
     * 关闭SQLManager连接
     * @throws SQLException 数据库异常
     */
    public void close() throws SQLException {
        this.connection.close();
    }
}
